package cn.lin.config;

import jakarta.annotation.PostConstruct;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * @author zxy
 * @date 2024/12/4 9:24
 */
@Configuration
public class RabbitConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback{

    @Autowired
    private RabbitTemplate rabbitTemplate ;
    /**
     * @author: zxy
     * @date: 2024/12/4 9:26
     * 具有确认回调和返回回调功能
     **/
    @PostConstruct
    public void init() {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
    }
    /**
     * @author: zxy
     * @date: 2024/12/4 9:26
     * 消息发送到交换机成功或失败调用这个方法
     **/
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("correlationData = " + correlationData + ", ack = " + ack + ", cause = " + cause);
    }
    /**
     * @author: zxy
     * @date: 2024/12/4 9:27
     * 队列失败会触发这个方法
     **/
    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        System.out.println("returnedMessage = " + returnedMessage);
    }
}
